from django.contrib.auth.models import AnonymousUser
from rest_framework import serializers

# 在问答首页显示问题列表序列化器
from question.models import Troubles, NavigationLabel, TroublesLabel, Respond, RespondComment
from users.models import User


class NavigationLabelSerializer(serializers.ModelSerializer):
    """导航标签序列化器"""

    class Meta:
        model = NavigationLabel
        fields = ('id', 'name')
        depth = 1


class TroublesLabelSerializer(serializers.ModelSerializer):
    """显示问题是什么标签分类的序列化器嵌套"""

    class Meta:
        model = TroublesLabel
        fields = ('navigation_label',)
        depth = 2


class UserSerializer(serializers.ModelSerializer):
    """返回用户信息嵌套"""

    class Meta:
        model = User

        fields = ('id', 'username', 'avatar')


class TroublesListSerializer(serializers.ModelSerializer):
    """问答信息"""
    troubleslabel_set = TroublesLabelSerializer(many=True, read_only=True)
    user = UserSerializer(read_only=True)

    class Meta:
        model = Troubles
        fields = ('id', 'title', 'user', 'answer_count', 'like_count', 'create_time', 'look_count'
                  , 'new_answer_name', 'new_answer_time', 'troubleslabel_set')
        extra_kwargs = {
            'id': {'read_only': True},
        }

class TroublesDetailSerializer(serializers.ModelSerializer):
    """问题详情页内容"""
    troubleslabel_set = TroublesLabelSerializer(many=True, read_only=True)
    user = UserSerializer(read_only=True)

    class Meta:
        model = Troubles

        fields = ('id','title', 'user', 'create_time', 'content'
                  , 'new_answer_time', 'troubleslabel_set','answer_count','like_count')
        extra_kwargs = {
            'id': {'read_only': True},
        }


class CommentSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)
    class Meta:
        model = RespondComment
        fields = ('content', 'user', 'id', 'create_time')
        extra_kwargs = {
            'id': {'read_only': True},
            # 'respond': {'write_only':True},
            'content': {'required': True},
        }

    def create(self, validated_data):
        respond_id = self.context['view'].kwargs['pk']

        user = self.context['request'].user

        if isinstance(user, AnonymousUser):
            raise serializers.ValidationError('请登录后操作')

        validated_data['user'] = user
        validated_data['respond_id'] = respond_id

        return super().create(validated_data)


class RespondSerializer(serializers.ModelSerializer):
    respondcomment_set = CommentSerializer(many=True, read_only=True)
    user = UserSerializer(read_only=True)

    class Meta:
        model = Respond
        fields = ('content', 'id', 'respondcomment_set', 'user','question_id')
        extra_kwargs = {
            'id': {'read_only': True},
            'content': {'required': True},


        }

    def create(self, validated_data):

        question_id = self.context['view'].kwargs['pk']

        user = self.context['request'].user

        # if isinstance(user, AnonymousUser):
        #     raise serializers.ValidationError('请登录后操作')

        validated_data['user'] = user
        validated_data['question_id'] = question_id

        return super().create(validated_data)



class SubmitQuestionSerializer(serializers.ModelSerializer):

    pass